import BaseSprite from "../../Base/BaseSprite";
import { EResType } from "../../Common/Enum";
import { ItemWord } from "./ItemWord";

const { ccclass, property } = cc._decorator;

export interface IWordData {
    type: EWordType;
    resType: EResType;
    value: string;
    size?: number;
    startPos?: cc.Vec2;
    endPos?: cc.Vec2;
    afterTime?: number;
    callback?: Function;
}

export enum EWordType {
    Font,
    Custom,
}

@ccclass
export class CustomWord extends BaseSprite {

    @property({ type: cc.Prefab, tooltip: "自定义" })
    private itemCustom: cc.Prefab = null;

    @property({ type: cc.Node, tooltip: "图片" })
    private custom: cc.Node = null;

    @property({ type: cc.Node, tooltip: "font" })
    private label: cc.Node = null;

    public setWordValue(data: IWordData) {
        this.custom.active = false;
        this.label.active = false;
        switch (data.type) {
            case EWordType.Custom:
                {
                    this.custom.active = true;
                    for (let i = 0; i < data.value.length; i++) {
                        const word = cc.instantiate(this.itemCustom);
                        word.parent = this.custom;
                        word.getComponent(ItemWord).setValue(data.resType, data.value[i], data.size);
                    }
                    setTimeout(() => {
                        this.custom.getComponent(cc.Layout).updateLayout();
                    }, 1000);
                }
                break;
            case EWordType.Font:
                {
                    this.label.active = true;
                    this.label.getComponent(cc.Label).fontSize = data.size;
                }
                break;
        }
        console.log(data.value);

        if (data.startPos && data.endPos) {
            this.node.scale = 0;
            this.node.setPosition(data.startPos);
            cc.tween(this.node).sequence(
                cc.scaleTo(0.2, 1).easing(cc.easeBackInOut()),
                cc.moveTo(data.afterTime, data.endPos),
                cc.scaleTo(0.2, 0).easing(cc.easeBackInOut()),
                cc.callFunc(() => {
                    this.node.destroy();
                    data.callback && data.callback();
                    data.callback = null;
                })
            ).start();
        }
    }
}